package com.hutao.medical.service.shiro;

import java.util.LinkedHashMap;  
import java.util.Map;  
  
import org.apache.shiro.spring.LifecycleBeanPostProcessor;  
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;  
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;  
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfiguration {
	
	  private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();  
	  
	    @Bean(name = "ShiroRealmImpl")  
	    public ShiroRealmImpl getShiroRealm() {  
	        return new ShiroRealmImpl();  
	    }  
	  
//	    @Bean(name = "shiroEhcacheManager")  
//	    public EhCacheManager getEhCacheManager() {  
//	        EhCacheManager em = new EhCacheManager();  
//	        em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");  
//	        return em;  
//	    }  
	  
	    @Bean(name = "lifecycleBeanPostProcessor")  
	    public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {  
	        return new LifecycleBeanPostProcessor();  
	    }  
	  
	    //去掉下面这段代码解决AOP二次代理问题
//	    @Bean  
//	    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {  
//	        DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();  
//	        daap.setProxyTargetClass(true);  
//	        return daap;  
//	    }  
	    

	    @Bean(name = "securityManager")  
	    public DefaultWebSecurityManager getDefaultWebSecurityManager() {  
	        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();  
	        dwsm.setRealm(getShiroRealm());  
	        //dwsm.setCacheManager(getEhCacheManager());  
	        return dwsm;  
	    }  
	  
	    //shiro注解
	    @Bean  
	    public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {  
	        AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();  
	        aasa.setSecurityManager(getDefaultWebSecurityManager());  
	        return new AuthorizationAttributeSourceAdvisor();  
	    }  
	  
	    
	    

	    @Bean(name = "shiroFilter")  
	    public ShiroFilterFactoryBean getShiroFilterFactoryBean() {  
	        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();  
	        shiroFilterFactoryBean  
	                .setSecurityManager(getDefaultWebSecurityManager());  
	        //没有登录跳到登录页面
	        //shiroFilterFactoryBean.setLoginUrl("/user/index");  
	        
	    	// 未授权界面;
	        //shiroFilterFactoryBean.setUnauthorizedUrl("/user/error");
	       
	        //不拦截请求
	        filterChainDefinitionMap.put("/**", "anon");
	        //filterChainDefinitionMap.put("/**", "authc");
	        shiroFilterFactoryBean  
	                .setFilterChainDefinitionMap(filterChainDefinitionMap);  
	        return shiroFilterFactoryBean;  
	    }  
	    
	    
	    
	
/*	private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();  
	  
    @Bean(name = "ShiroRealmImpl")  
    public ShiroRealmImpl getShiroRealm() {  
        return new ShiroRealmImpl();  
    }  
  
//    @Bean(name = "shiroEhcacheManager")  
//    public EhCacheManager getEhCacheManager() {  
//        EhCacheManager em = new EhCacheManager();  
//        em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");  
//        return em;  
//    }  
  
    @Bean(name = "lifecycleBeanPostProcessor")  
    public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {  
        return new LifecycleBeanPostProcessor();  
    }  
  
    //去掉下面这段代码解决AOP二次代理问题
//    @Bean  
//    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {  
//        DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();  
//        daap.setProxyTargetClass(true);  
//        return daap;  
//    }  
    

    @Bean(name = "securityManager")  
    public DefaultWebSecurityManager getDefaultWebSecurityManager() {  
        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();  
        dwsm.setRealm(getShiroRealm());  
        //dwsm.setCacheManager(getEhCacheManager());  
        return dwsm;  
    }  
  
    //shiro注解
    @Bean  
    public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {  
        AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();  
        aasa.setSecurityManager(getDefaultWebSecurityManager());  
        return new AuthorizationAttributeSourceAdvisor();  
    }  
  
    
    

    @Bean(name = "shiroFilter")  
    public ShiroFilterFactoryBean getShiroFilterFactoryBean() {  
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();  
        shiroFilterFactoryBean  
                .setSecurityManager(getDefaultWebSecurityManager());  
        //没有登录跳到登录页面
        shiroFilterFactoryBean.setLoginUrl("/user/toLogin");  
        
    	// 未授权界面;
        //shiroFilterFactoryBean.setUnauthorizedUrl("/user/error");
        //不拦截请求
        
//        filterChainDefinitionMap.put("/user/toLogin", "anon");
//       // filterChainDefinitionMap.put("user/authority/index", "anon");
//        filterChainDefinitionMap.put("/user/logout", "anon");
//        filterChainDefinitionMap.put("/user/userLogin", "anon");
//        filterChainDefinitionMap.put("/static/**", "anon");
//       // filterChainDefinitionMap.put("/print/queryMedPage", "anon");
        filterChainDefinitionMap.put("/**", "anon");
        //filterChainDefinitionMap.put("/**", "authc");
        shiroFilterFactoryBean  
                .setFilterChainDefinitionMap(filterChainDefinitionMap);  
        return shiroFilterFactoryBean;  
    }
    */
}
